package com.imay.videorecord.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imay.videorecord.R;
import com.imay.videorecord.utils.DensityUtils;
import com.imay.videorecord.view.AutoAdjustRecylerView;

import java.util.ArrayList;
import java.util.List;




/**
 * Created by Administrator on 2016/11/7.
 */
public class FilterAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private AutoAdjustRecylerView mAutoAdjustRecylerView;
    private List<String> names;
    private int selectedPostion = 0;
    private int screenW = 0;


    public FilterAdapter(Context mContext, AutoAdjustRecylerView mAutoAdjustRecylerView) {
        this.mContext = mContext;
        this.mAutoAdjustRecylerView = mAutoAdjustRecylerView;
        names = new ArrayList<>();
        screenW = DensityUtils.getScreenW((Activity) mContext);

    }

    public List<String> getNames() {
        return names;
    }

    public void setNames(List<String> names) {
        this.names = names;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filter, parent, false);
        FilterViewHolder testViewHolder = new FilterViewHolder(mAutoAdjustRecylerView,itemView);
        return testViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FilterViewHolder viewHolder = (FilterViewHolder) holder;
        viewHolder.tv_name.setText(names.get(position)+"");
        if(position==selectedPostion){
            viewHolder.tv_name.setSelected(true);
        }else{
            viewHolder.tv_name.setSelected(false);
        }
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) viewHolder.tv_name.getLayoutParams();
        if(position==0){
            params.setMargins((screenW-DensityUtils.dip2px(mContext,37))/2,0,0,0);
        }else{
            params.setMargins(0,0,0,0);
        }
        viewHolder.tv_name.setLayoutParams(params);
    }

    @Override
    public int getItemCount() {
        return names.size();
    }

    public int getSelectedPostion() {
        return selectedPostion;
    }

    public void setSelectedPostion(int selectedPostion) {
        this.selectedPostion = selectedPostion;
    }


    class FilterViewHolder extends AutoAdjustRecylerView.AbstractAutoAdjustViewHolder {

        public TextView tv_name;

        public FilterViewHolder(AutoAdjustRecylerView autoAdjustRecylerView, View view) {
            autoAdjustRecylerView.super(view);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void initView(View view) {
            // TODO Auto-generated method stub
            tv_name = (TextView) view.findViewById(R.id.tv_name);
        }
    }
}
